package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.paotui.dao.model.RunfastShopper;
		import com.runfast.paotui.service.RunfastCuseraddressService;
        import com.runfast.paotui.service.RunfastShopperService;
        import com.runfast.waimai.dao.model.RunfastGoodsSellChildren;
		import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;

		import com.runfast.waimai.service.RunfastBusinessService;

		import com.runfast.waimai.service.RunfastGoodsSellChildrenService;
		import com.runfast.waimai.service.RunfastGoodsSellRecordService;
		import com.runfast.waimai.util.AESUtil;
		import com.runfast.waimai.util.MyCustomDateEditor;
		import com.runfast.waimai.util.MyUtil;
		import org.springframework.amqp.core.AmqpTemplate;
		import org.springframework.stereotype.Controller;
		import org.springframework.ui.ModelMap;
		import org.springframework.web.bind.WebDataBinder;
		import org.springframework.web.bind.annotation.*;

		import javax.annotation.Resource;
		import java.io.IOException;
		import java.util.Date;
		import java.util.List;
		import java.util.Map;


@Controller
@RequestMapping("/driver/order/*")
public class DriverOrderController extends DriverBaseController {
	@Resource
	private RunfastGoodsSellRecordService goodsSellRecordService;
	//RunfastGoodsSellRecord外卖订单
	@Resource
	private RunfastGoodsSellChildrenService goodsSellRecordChildrenService;
	//RunfastGoodsSellChildren外卖订单记录
	@Resource
	private RunfastBusinessService businessService;
	//商家信息RunfastBusiness
	@Resource
	private RunfastCuseraddressService cuserAddressService;
	@Resource
    private RunfastShopperService shopperService;
	//用户地址RunfastCuseraddress
	@Resource
	private AmqpTemplate rabbitTemplate; //rabbitmq 发送
	@InitBinder//限定只有传参包含Application属性时才执行添加此操作
	public void initBinder(WebDataBinder binder) {
		//binder.setValidator(validator);
		//注册转换器
		binder.registerCustomEditor(Date.class, new MyCustomDateEditor());
	}

//*
//* 订单详细      +商品规格返回
//* @throws IOException

	@RequestMapping(value="detail",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap detail(@RequestParam(value="id",required=false) Integer id,String token) throws IOException{
	    //Integer id 定单id
        //Integer id1骑手id
		ModelMap model = new ModelMap();
		RunfastShopper sp=super.getLoginShopper(token);
	//	=shopperService.get(id1);
		if(sp!=null&&id!=null){
			//将getinto 改成了findByid，应该是orderid查询外卖订单规格信息
			RunfastGoodsSellRecord gr = goodsSellRecordService.get(id);
			//	List<RunfastGoodsSellChildren> sublist = goodsSellRecordChildrenService.findbyorderid(gr.getId());

			if(gr!=null){
				List<RunfastGoodsSellChildren> grclist= goodsSellRecordChildrenService.findByOrderCode(gr.getOrderCode());///查询订单子选项
				//OrderCode定单号
				if(grclist!=null&&grclist.size()>0){
					for(RunfastGoodsSellChildren grc : grclist){
						model.put("GoodsSellRecordChildren", grc);
					}
				}
				gr=MyUtil.replaceTag(gr); //骑手的坐标定点
				//GoodsSellRecord gr =goodsSellRecordService.findByOrderCode(orderCode);  //订单信息
				Map<String,Object> loc = MyUtil.getGDlocFromBD( gr.getBusinessAddressLng(), gr.getBusinessAddressLat()) ;//设置商家的经度，纬度
				Map<String,Object> userLoc = MyUtil.getGDlocFromBD(gr.getUserAddressLng() , gr.getUserAddressLat());//用户的经度，纬度

//				System.out.println(gr.getUserAddressLng() + gr.getUserAddressLat());
//				System.out.println(userLoc);
				//System.out.println("addr = "+ gr.getUserAddress() + gr.getAddress() );
				if(userLoc != null&&userLoc.size()>0){

					gr.setUserAddressLat(userLoc.get("lat").toString());
					gr.setUserAddressLng(userLoc.get("lng").toString());
//					System.out.println("www设置好坐标lat" + gr.getUserAddressLat() +"lng = " + gr.getUserAddressLng());

				}else{
					System.out.print("转换坐标失败");
				}

				if(loc != null&&loc.size()>0){

					gr.setBusinessAddressLat(loc.get("lat").toString());
					gr.setBusinessAddressLng(loc.get("lng").toString());
					System.out.println("设置好坐标lat" + gr.getBusinessAddressLat() +"lng = " + gr.getBusinessAddressLng());

				}

				else{
					//LOG.info("百度坐标转高德坐标失败");
				}//把商家的坐标转换成高德的坐标
				//
				if(sp.getLongitude()!=null&&sp.getLatitude()!=null&&gr.getBusinessAddressLng()!=null&&gr.getBusinessAddressLat()!=null){
					Double shopdis=MyUtil.getDistance(Double.parseDouble(sp.getLongitude()), Double.parseDouble(sp.getLatitude()), Double.parseDouble(gr.getBusinessAddressLng()), Double.parseDouble(gr.getBusinessAddressLat()));
			//		this.rabbitTemplate.convertAndSend("hellowo", Math.round(shopdis)); //发送队列中
					model.put("shopbdis", Math.round(shopdis));//骑手到商家距离
				}
				//把商家的坐标转换成高德的坐标

//				if(gr.getBusinessId()!=null&&ComUtil.isEmpty(gr.getBusinessAddressLat())){
//					Business bus = businessService.get(gr.getBusinessId());
//					if(bus!=null){
//						gr.setBusinessAddressLng(bus.getLongitude());
//						gr.setBusinessAddressLat(bus.getLatitude());
//					}
//				}
//				if(gr.getUserAddressId()!=null&&ComUtil.isEmpty(gr.getUserAddressLat())){
//					CuserAddress addr = cuserAddressService.get(gr.getUserAddressId());
//					if(addr!=null){
//						gr.setUserAddressLat(addr.getLatitude());
//						gr.setUserAddressLng(addr.getLongitude());
//					}
//				}
//				//����---------��ɾ��-----------
//				if(ComUtil.isEmpty(gr.getBusinessAddressLat())){
//					gr.setBusinessAddressLat(""+22.797809);
//				}
//				if(ComUtil.isEmpty(gr.getBusinessAddressLng())){
//					gr.setBusinessAddressLng(""+108.359302);
//				}
//				if(ComUtil.isEmpty(gr.getUserAddressLat())){
//					gr.setUserAddressLat(""+22.797609);
//				}
//				if(ComUtil.isEmpty(gr.getUserAddressLng())){
//					gr.setUserAddressLng(""+108.359202);
//				}
//				//����---------��ɾ��-----------
			}
			//getDistance()为String类型 ，要用double进行比较，round为long类型，结果要为String类型，再次转换
			gr.setDistance(String.valueOf(Math.round(Double.parseDouble(gr.getDistance()))));
	//		this.rabbitTemplate.convertAndSend("hellowo", gr); //发送队列中
			model.put("detail", gr);
		}

		return model;
	}

}
